<html>
<body>

<?php 

$agency_file = "extracted_agencies.csv";
$taxonomy_file = "extracted_taxonomies.csv";
$max_runtime = 6000;
$csv_delim = ",";


echo "Maximum script runtime set to ".$max_runtime." seconds.<BR>";
set_time_limit($max_runtime);
error_reporting('IGNORE_REPEATED_ERRORS');


$conn = mysql_connect ("localhost", "root", "") or die ('Error: ' . mysql_error());
$db = mysql_select_db("QA");
//echo "ping db: ".mysql_ping($conn)."<BR>";

$handle = @fopen($taxonomy_file, "r");
echo "Taxonomy file opened: ".$taxonomy_file." with handle ".$handle."<BR>";
if ($handle) {
        //echo "Working on the handle<BR>";
        $file_lines = 0;
        $insert_executed = 0;
        $source = null;
        $buftok[] = null;
	while (!feof($handle)){
                $select_results = null;
		$bufferin = fgets($handle);
                //echo "bufferin: ".$bufferin."<BR>";
                $bufferin = trim($bufferin);
                //echo "bufferintrim: ".$bufferin."<BR>";
                //echo "bufferintrimstrlen: ".strlen($bufferin)."<BR>";
                if (strlen($bufferin)>0){
         		$buftokin =  explode($csv_delim, $bufferin);
                        //echo "exploded buffer: ".$buftokin."<BR>";
                }else{
                        $buftokin = array();
                }

                //echo "working on buftokin0:".$buftokin[0]."  buftokin1: ".$buftokin[1]."<BR>";
                if(strlen(trim($buftokin[1]))>0){
                    $query_string = "SELECT Taxonomy_Description FROM QA.Taxonomies WHERE Taxonomy_Description LIKE \"".mysql_real_escape_string($buftokin[0])."\";";
                    //echo "Query: ".$query_string."<BR>";
                    //echo "query call starting<BR>";
                    $select_results = mysql_query($query_string);
                    //echo "Query run<BR>";
                    //echo "Select Results: ".$select_results.", ".mysql_result($select_results,0)."<BR>";
                    if(!mysql_result($select_results,0)){
                         $query_string = "INSERT INTO QA.Taxonomies (Taxonomy_Description) VALUES (\"".mysql_real_escape_string($buftokin[0])."\");";
                         //echo "Query: ".$query_string."<BR>";
                         $insert_results = mysql_query($query_string);
                         //echo "Insert results: ".mysql_results(insert_results,0)."<BR>";
                         $insert_executed++;
                    }
                    $file_lines++;
                    //if($file_lines>2){exit;}
                }
	}

     fclose($handle);   
}

echo "<BR><HR><BR>";
echo "Taxonomies procesed with ".$file_lines." file lines processed and ".$insert_executed." rows inserted.<BR>";
echo "<BR><HR><BR>";

$handle = @fopen($agency_file, "r");
echo "Agency file opened: ".$agency_file."<BR>";
if ($handle) {
        $insert_executed = 0;
        $file_lines = 0;
        $source = null;
        $buftok[] = null;
	while (!feof($handle)){
		$bufferin = fgets($handle);
                $bufferin = trim($bufferin);
                if (strlen($bufferin)>0){
                        //check if its in the db, if not add
                        $query_string = "SELECT count(*) FROM Agencies WHERE Agency_Description LIKE '".mysql_real_escape_string($bufferin)."'";
                        //echo "Query: ".$query_string."<BR>";
                        $select_results = mysql_query($query_string);
                        //echo "selectresults: ".mysql_result($select_results,0)."<BR>";
                        if(mysql_result($select_results,0)==0){
                             $query_string = "INSERT INTO Agencies (Agency_Description) VALUES ('".mysql_real_escape_string($bufferin)."')";
                             //echo "Query: ".$query_string."<BR>";
                             $insert_results = mysql_query($query_string);
                             //echo "Insert results: ".$insert_results."<BR>";
                             $insert_executed++;
                        }
                        $file_lines++;
                }else{
                        $buftokin = array();
                }                
	}

     fclose($handle);   
}

echo "Agencies procesed with ".$file_lines." file lines processed and ".$insert_executed."rows inserted.<BR>";


?><BR>

</body>
</html>